package cn.wolfcode.ssm.web.controller;

import cn.wolfcode.ssm.domain.Department;
import cn.wolfcode.ssm.qo.QueryObject;
import cn.wolfcode.ssm.service.IDepartmentService;
import cn.wolfcode.ssm.util.RequireAnnotation;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/department")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentServlet;

    @RequireAnnotation(name = "部门页面",expression = "department:list")
    @RequestMapping("list")
    public String listAll(Model model, QueryObject qo){
        PageInfo<Department> pageInfo = departmentServlet.query(qo);
        model.addAttribute("pageInfo",pageInfo);
        return "department/list";
    }

    @RequireAnnotation(name = "部门删除",expression = "department:delete")
    @RequestMapping("delete")
    public String delete(Long id){
        departmentServlet.deleteById(id);
        return "redirect:/department/list";
    }

    @RequireAnnotation(name = "部门编辑/保存",expression = "department:saveOrUpdate")
    @RequestMapping("input")
    public String input(Model model,Long id){

        if(id != null){
            Department department = departmentServlet.selectById(id);
            model.addAttribute("department",department);
        }
        return "department/input";
    }

    @RequireAnnotation(name = "部门编辑/保存",expression = "department:saveOrUpdate")
    @RequestMapping("saveOrUpdate")
    public String saveOrUpdate(Model model,Department department){

        if(department.getId() != null){
            departmentServlet.updateById(department);
        }else {
            departmentServlet.save(department);
        }
        return "redirect:/department/list";
    }

}
